﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SS.Ynote.Classic.UI.Forms
{
    public partial class PluginManager : Form
    {
        public PluginManager()
        {
            InitializeComponent();
            this.lstplugins.FullRowSelect = true;
            this.StartPosition = FormStartPosition.CenterScreen;
            LoadPluginList();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ImportPlugin();
        }
        static void ImportPlugin()
        {
            using (OpenFileDialog o = new OpenFileDialog())
            {
                o.Filter = "Ynote Plugins (*.dll)|*.dll";
                o.ShowDialog();
                if (!string.IsNullOrEmpty(o.FileName))
                    CopyAndPrint(o.FileName, Application.StartupPath + @"\Plugins\" + System.IO.Path.GetFileName(o.FileName));
            }
        }
        static void CopyAndPrint(string file1, string file2)
        {
            try
            {
                System.IO.File.Copy(file1, file2, true);
                DialogResult result = MessageBox.Show("Plugin Imported. Restart To See Effects\r\nRestart Now?", "Success.", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                    Application.Restart();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://ynoteplugins.codeplex.com");
        }
        void LoadPluginList() 
        {
            foreach (IPlugin p in IPluginManager.Plugins)
            {
                var item = new ListViewItem(new[] { p.Name, p.Version, p.Author,p.Description });
                lstplugins.Items.Add(item);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
